import Vue from 'vue';
import Router from 'vue-router';
import Welcome from '@/views/Welcome.vue';
import User from '@/views/User.vue';
import Permission from '@/views/Permission.vue';
import Login from '@/views/Login.vue';
import TableData from '@/views/TableData.vue';

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/welcome'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/welcome',
      name: 'Welcome',
      component: Welcome
    },
    {
      path: '/user',
      name: 'User',
      component: User
    },
    {
      path: '/permission',
      name: 'Permission',
      component: Permission
    },
    {
      path: '/table-data',
      name: 'TableData',
      component: TableData
    }
  ]
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('isLogin') === 'true';
  
  if (to.path === '/login') {
    // 已登录状态下访问登录页，重定向到首页
    if (isLogin) {
      next('/');
    } else {
      next();
    }
  } else {
    // 未登录状态下访问其他页面，重定向到登录页
    if (!isLogin) {
      next('/login');
    } else {
      next();
    }
  }
});

export default router;